-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.c
More file actions
34 lines (29 loc) · 736 Bytes
/
Solution.c
File metadata and controls
34 lines (29 loc) · 736 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
int removeDuplicates(int arr[], int n) {
if (n == 0) return 0;
int j = 0; // Index for the next unique element
for (int i = 1; i < n; i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}
int main() {
int n;
printf("Enter the size of the sorted array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements of the sorted array: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int newSize = removeDuplicates(arr, n);
printf("Array after removing duplicates: ");
for (int i = 0; i < newSize; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}